<?php
App::uses('Controller', 'Controller');

class AppController extends Controller {

	public $components = array(
		'Cookie', 'Session', 'DebugKit.Toolbar',
		'Auth' => array(
			'loginRedirect' => array(
				'controller' => 'posts',
				'action' => 'index'
			),
			'logoutRedirect' => array(
				'controller' => 'posts',
				'action' => 'home',
				'admin'	=> false
			),
			'authenticate' => array(
				'Form' => array(
					'passwordHasher' => 'Blowfish'
				)
			),
			'authorize' => array('Controller')
		)
	
	);
	public $helpers = array(
		'Html', 'Form', 'Text', 'Session', 'Js' => array('Jquery')
	);

	public function beforeFilter() {
		//AUTH
		$this->Auth->allow('index', 'show', 'search');
		// ADMIN LAYOUT
		if (isset($this->params['prefix']) && $this->params['prefix'] == 'admin') {
			$this->layout = 'admin';
		};

		//ak je ajaxovy request nastavime layout
		if ($this->request->is('ajax')) {
			$this->layout = 'ajax';
			Configure::write('debug', Configure::read('ajaxDebug'));
		}
	}

	public function isAuthorized($user) {
		// Admin can access every action
		if (isset($user['role']) && $user['role'] === 'admin') {
			return true;
		}
		// Default deny
		return false;
	}
}
